<?php

namespace App\Http\Controllers\Api\Mobile;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Models\ErrorPath;
use App\Models\Domain;
use App\Models\Complain;
use App\Services\CdnService;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;


class ApiController extends Controller
{
    /**
     * 获取落地域名接口
     *
     * @param Request $request
     * @return void
     */
    public function getBorn(Request $request){
        //获取落地
        $born_domain = Cache::rememberForever('born_domain',  function () {
            //落地有可能没有了，所以加锁防止没有时缓存穿透
            return Cache::lock('born_domain')->get(function () {
                // 获取无限期锁并自动释放...
                return Domain::show()->born()->orderBy('sort')->value('domain');
            });
        });
        //缓存为空
        if(!$born_domain){
            //清空缓存
            Cache::forget('born_domain');
            // 没有落地域名了
            $data['born_domain'] = '';
        }
        else{
            $data['born_domain'] = $born_domain;
        }
        return $this->success($data);
    }

    /**
     * 获取视频cdn
     *
     * @param Request $request
     * @return void
     */
    public function getVideoCdns(Request $request){
        $cdn_service = new CdnService();
        $video_cdns = $cdn_service->getVideoCdn();
        if($video_cdns['state']!='success' || ($video_cdns['video_cdn1']=='' && $video_cdns['video_cdn2']=='')){
            return errorGo($video_cdns['state']);
        }
        $data['video_cdns'] = $video_cdns;
        return $this->success($data);
    }

    /**
     * 图片视频路径异常上报接口
     *
     * @param Request $request
     * @return void
     */
    public function urlError(Request $request){
        if($request->has('code') && $request->code){
            $code_data = decodeCode($request->code);
            //解析后的code是数组
            if(is_array($code_data) && count($code_data)>3){
                //正常逻辑代码
                ErrorPath::create([
                    'domain'=>parse_url($request->path)['host'],
                    'path' => $request->path,
                    'ip' =>getIp(),
                    'type'=>$request->type??1,
                    'ua'=>$_SERVER['HTTP_USER_AGENT'],
                ]);
                return $this->success('已记录');
            }
            else{
                return $this->errMsg('有情况，请通过正常渠道访问！');
            }
        }
        return $this->errMsg('有情况，请通过正常渠道访问');
    }


    /**
     * 投诉
     *
     * @param Request $request
     * @return void
     */
    public function complain(Request $request){
        $code_data = decodeCode($request->code);
        $ip = getIp();
        Complain::create([
            'agent_id'=>$code_data[1],
            'code'=>$request->code,
            'ip'=>$ip,
            'url'=>$request->fullUrl(),
            'forbiden'=>1,
            'openid'=>$code_data[2],
            'ua'=>$_SERVER['HTTP_USER_AGENT']
        ]);
        cache()->forget("bad_openid:{$code_data[2]}");
        return $this->success('success');
    }

    /**
     * 
     */
    public function clearingUaLink(){
        //解除24小时未登录的绑定关系
        $data =  DB::select('SELECT `openid`,  `created_at` FROM view_counts WHERE id in(select MAX(id)  from view_counts group BY openid ) ');
        foreach ($data as $item){
            if(Carbon::now()->addDays(-1)>$item->created_at){
                cache()->forget("openid_ua:{$item->openid}");
            }
        }
        \Log::debug('释放用户 openid 和 ua 的关联');
        return 0;
    }
}
